package com.chenjim.glrecorder.surfacetexture;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.opengl.GLSurfaceView;
import android.widget.Button;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;

public class CameraGLSurfaceView extends GLSurfaceView {
    private final CameraRendererEx mRenderer;
    private SurfaceTexture mSurfaceTexture;

    public CameraGLSurfaceView(Context context) {
        super(context);
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        setEGLContextClientVersion(3);
        setEGLConfigChooser((egl, display) -> {
            // 请求多重采样配置
            int[] configSpec = {
                    EGL10.EGL_RED_SIZE, 8,
                    EGL10.EGL_GREEN_SIZE, 8,
                    EGL10.EGL_BLUE_SIZE, 8,
                    EGL10.EGL_ALPHA_SIZE, 8,
                    EGL10.EGL_DEPTH_SIZE, 16,
                    EGL10.EGL_SAMPLE_BUFFERS, 1,
                    EGL10.EGL_SAMPLES, 4, // 4x MSAA
                    EGL10.EGL_NONE
            };
            EGLConfig[] configs = new EGLConfig[1];
            int[] numConfig = new int[1];
            egl.eglChooseConfig(display, configSpec, configs, 1, numConfig);
            return configs[0];
        });
        mRenderer = new CameraRendererEx(context, this);
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }


    public void setSuperResolutionUniform(Button button) {
        if (mRenderer.getmSuperResolutionUniform() == 1) {
            mRenderer.setmSuperResolutionUniform(0);
            button.setText("正常效果");
        } else {
            mRenderer.setmSuperResolutionUniform(1);
            button.setText("增强效果");
        }
    }

    public SurfaceTexture getSurfaceTexture() {
        return mSurfaceTexture;
    }

    public void setSurfaceTexture(SurfaceTexture surfaceTexture) {
        mSurfaceTexture = surfaceTexture;
    }
}